package org.mozhu.mboot.core.web;

import lombok.extern.slf4j.Slf4j;
import org.mozhu.mboot.core.ApplicationContextWrapper;
import org.mozhu.mboot.core.template.freemarker.FreemarkerTemplateRender;
import org.mozhu.mboot.core.web.bind.ControllerDefaultsSupport;
import org.mozhu.mboot.core.web.controller.ConfigurableController;
import org.mozhu.mboot.core.web.converter.String2DateConverter;
import org.mozhu.mboot.core.web.converter.String2LocalDateConverter;
import org.mozhu.mboot.core.web.resource.WebResourceConfigurerAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Import(WebResourceConfigurerAutoConfiguration.class)
@Slf4j
public class WebApplicationAutoConfiguration {

    @Bean
    public ApplicationContextWrapper applicationContextWrapper() {
        return new ApplicationContextWrapper();
    }

    @Bean
    @ConditionalOnBean(ConfigurableController.class)
    public ControllerDefaultsSupport controllerDefaultsSupport() {
        return new ControllerDefaultsSupport();
    }

    @Bean
    @ConditionalOnMissingBean
    public FreemarkerTemplateRender freemarkerTemplateRender() {
        return new FreemarkerTemplateRender();
    }
    
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {

            @Override
            public void addFormatters(FormatterRegistry registry) {
                registry.addConverter(new String2LocalDateConverter());
                registry.addConverter(new String2DateConverter());
            }

        };
    }
}
